package com.yunhe.rentcars.commons.util.mail;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.*;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Properties;

/**
 * 
 * @author tianxuan 本类只有这么一个方法，用来发邮件！
 */
public class MailUtils {

	public static void main(String[] args) throws GeneralSecurityException, IOException, MessagingException {

		Mail mail = new Mail("2603459843@qq.com", "2603459843@qq.com", "账号激活", "ljh2603459843：<br><a href=\"https://www.baidu.com\">点我激活账号</a>");
		/*mail.addToAddress("575687823@qq.com");  //添加第二个收件人
		AttachBean attachBean = new AttachBean("img001",new File("E:\\aa.jpeg"), "美女.jpeg");
		mail.addAttach(attachBean);*/
		mail(mail);

	}


	public static void mail(Mail mail) throws GeneralSecurityException, IOException, MessagingException {
		Session session = createSession("smtp.qq.com", "2603459843@qq.com", "ytdggvouymiyeadb");
		send(session,mail);
	}

	/**
	 * 发送指定的邮件
	 *
	 * @param mail
	 */
	public static void send(Session session, final Mail mail) throws MessagingException,
			IOException {

		MimeMessage msg = new MimeMessage(session);// 创建邮件对象
		msg.setFrom(new InternetAddress(mail.getFrom()));// 设置发件人
		msg.addRecipients(RecipientType.TO, mail.getToAddress());// 设置收件人

		// 设置抄送
		String cc = mail.getCcAddress();
		if (!cc.isEmpty()) {
			msg.addRecipients(RecipientType.CC, cc);
		}

		// 设置暗送
		String bcc = mail.getBccAddress();
		if (!bcc.isEmpty()) {
			msg.addRecipients(RecipientType.BCC, bcc);
		}

		msg.setSubject(mail.getSubject());// 设置主题

		MimeMultipart parts = new MimeMultipart();// 创建附件集对象

		MimeBodyPart part = new MimeBodyPart();// 创建一个附件
		part.setContent(mail.getContent(), "text/html;charset=utf-8");// 设置邮件文本内容
		parts.addBodyPart(part);// 把附件添加到附件集


		// 添加附件
		List<AttachBean> attachBeanList = mail.getAttachs();// 获取所有附件
		if (attachBeanList != null) {
			for (AttachBean attach : attachBeanList) {
				MimeBodyPart attachPart = new MimeBodyPart();// 创建一个部件
				attachPart.attachFile(attach.getFile());// 设置附件文件
				attachPart.setFileName(MimeUtility.encodeText(attach.getFileName()));// 设置附件文件名
				String cid = attach.getCid();
				if(cid != null) {
					attachPart.setContentID(cid);
				}
				parts.addBodyPart(attachPart);
			}
		}

		msg.setContent(parts);// 给邮件设置内容
		Transport.send(msg);// 发邮件
	}

	public static Session createSession(String host, final String username, final String password) throws GeneralSecurityException {
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", host);// 指定主机
		props.setProperty("mail.smtp.auth", "true");// 指定验证为true

		// 关于QQ邮箱，还要设置SSL加密，加上以下代码即可
		MailSSLSocketFactory sf = new MailSSLSocketFactory();
		sf.setTrustAllHosts(true);
		props.put("mail.smtp.ssl.enable", "true");
		props.put("mail.smtp.ssl.socketFactory", sf);

		// 创建验证器
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		};
		
		// 获取session对象
		return Session.getDefaultInstance(props, auth);
	}
	

}



